package alkaid.eis.sys.controller;

import static org.springframework.util.StringUtils.hasText;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import alkaid.eis.sys.entity.Option;
import alkaid.eis.sys.repository.OptionRepository;
import alkaid.eis.sys.service.LongTextService;
import alkaid.eis.sys.service.OptionService;
import alkaid.eis.sys.service.OptionSpecifications;
import lombok.RequiredArgsConstructor;

/**
 * 设置项接口
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@RestController
@RequestMapping("/options")
@RequiredArgsConstructor
public class OptionController {
    private final OptionRepository optionRepository;
    private final OptionService optionService;
    private final LongTextService longTextService;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Long create(@RequestBody OptionDto optionDto) {
        Option entity = optionRepository.save(OptionMapper.INST.toEntity(optionDto));
        return entity.getId();
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@PathVariable("id") Long id, @RequestBody OptionDto optionDto) {
        optionService.update(id, optionDto.getCode(), optionDto.getDescription());
    }

    @PutMapping("/{id}/value")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateValue(@PathVariable("id") Long id, @RequestBody String value) {
        Option option = optionService.findById(id);
        longTextService.saveContentByRefId(option.getId(), value);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void remove(@PathVariable("id") Long id) {
        optionService.deleteById(id);
    }

    @GetMapping("/{code}/value")
    public String getValueByCode(@PathVariable("code") String code) {
        Option entity = optionService.findByCode(code);
        return longTextService.findContentByRefId(entity.getId());
    }

    @GetMapping
    public Page<OptionDto> getPage(@RequestParam(value = "description", required = false) String description, Pageable pageable) {
        Specification<Option> where = OptionSpecifications.empty();
        if (hasText(description)) {
            where = where.and(OptionSpecifications.descriptionLike(description));
        }

        Page<Option> page = optionRepository.findAll(where, pageable);
        return page.map(OptionMapper.INST::toDto);
    }
}
